<?php


namespace app\home\model;


use app\admin\model\NewsCommunicationDetail;
use think\Db;

class NewsCommunication extends Base
{
    protected $insert = [
        'create_time' => NOW_TIME,
    ];

    /**
     * @param $permission 权限类别
     * @param $userId   用户id
     * @param int $status   状态，0待分发，1轮阅中，2已归档
     * @param int $leng 数据长度
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($permission,$userId,$status = 0,$leng = 0,$query_time = "",$search_value = "")
    {
        $map = [
            'b.permission' => $permission,
            'b.userid' => $userId,
            'b.status' => $status,
        ];
        if(!empty($query_time)) {
            $firstDay = strtotime(date('Y-m-01 00:00:00',$query_time));
            $lastDay = strtotime(date('Y-m-d 23:59:59', strtotime(date('Y-m-01', $query_time). ' 1 month -1 day')));
            $map['b.create_time'] = ['between',[$firstDay,$lastDay]];
        }
        $builder = Db::table('zm_news_communication')->alias('a')
            ->join('zm_news_communication_detail b','a.id = b.communication_id','LEFT')
            ->where($map);
        if(!empty($search_value)) {
            $builder->where(function ($query) use ($search_value){
                $query->where('a.title','like',"%".$search_value."%")
                    ->whereOr('b.userid','like',"%".$search_value."%");
            });
        }
        $list = $builder->field('b.id,a.title,a.type,a.create_time,b.permission,b.userid,b.communication_id,b.status')
            ->order('b.create_time desc')
            ->limit($leng,8)
            ->select();
        int_to_string($list,['type' => [1 => "行政", 2 => "党建", 3 => "工会", 4 => "其它" ]]);
        return $list;
    }


    /**
     * 拟办意见列表
     * @param $userId
     * @param string $status
     * @param int $leng
     * @param string $query_time
     * @param string $search_value
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOpinionList($userId,$status = "all",$leng = 0,$query_time = "",$search_value = "")
    {
        $map = ['operate_user' => $userId,'status' => ['gt',-1]];
        if($status != "all") {
            $map['operate_status'] = $status;
        }
        if(!empty($search_value)) {
            $map['title'] = ["like","%".$search_value."%"];
        }
        if(!empty($query_time)) {
            $firstDay = strtotime(date('Y-m-01 00:00:00',$query_time));
            $lastDay = strtotime(date('Y-m-d 23:59:59', strtotime(date('Y-m-01', $query_time). ' 1 month -1 day')));
            $map['create_time'] = ['between',[$firstDay,$lastDay]];
        }
        $list = Db::name('news_communication')
            ->where($map)
            ->order('create_time desc')
            ->limit($leng,8)
            ->select();
        int_to_string($list,['type' => [1 => "行政", 2 => "党建", 3 => "工会", 4 => "其它" ]]);
        int_to_string($list,['operate_status' => [0 => "待审批", 1 => "已通过", 2 => "已驳回"]]);
        return $list;
    }

    /**
     * 拟办详情
     * @param $id
     * @return \think\db\Query
     */
    public function getOpinionDetail($id)
    {
        $typeArr = [1 => "行政", 2 => "党建", 3 => "工会", 4 => "其它" ];
        $statusArr = [0 => "待审批", 1 => "已通过", 2 => "已驳回"];
        $detail = Db::name('news_communication')
            ->where('id',$id)
            ->where('status','gt',-1)
            ->find();
        $detail['type_text'] = isset($typeArr[$detail['type']]) ? $typeArr[$detail['type']] : "无";
        $detail['operate_status_text'] = isset($statusArr[$detail['operate_status']]) ? $statusArr[$detail['operate_status']] : "无";
        if($detail['attachment']) {
            $attachment = json_decode($detail['attachment']);
            $files = [];
            foreach ($attachment as $value) {
                $files[] = Db::table('zm_file')->where(['id' => $value])->find();
            }
            $detail['attachment'] = $files;
        }else {
            $detail['attachment'] = [];
        }
        return $detail;
    }
}